import com.dinmax.mediacenter.*

class MenuBiblioteca {
	private var _bibliotecas:Array; // albums
	private var _clipsBibliotecas:Array; //movie clips
	private var lsMenu:MenuItemListener;
	private var _acordion:MovieClip;
	private var _clip:MovieClip;
	private var _clipArriba:MovieClip;
	private var _mask:MovieClip;
	
	function MenuBiblioteca(parent:MovieClip, acordion:MovieClip){
		_acordion=acordion;
		_clip=parent;
		_clipsBibliotecas=new Array();
		dibujarMenu();
	}
	
	public function getClips():Array{
		return _clipsBibliotecas;
	}
	public function tocarAlbum(indice:Number){
		lsMenu.clipPress(_clipsBibliotecas[indice]);
	}
	public function borrarBibliotecas(){
		_mask.removeMovieClip();
		for(var sNested:String in _clipArriba){
		_clipArriba[sNested].removeMovieClip();
		_clipArriba.removeMovieClip();
		}
		_clipsBibliotecas.splice(0);
	}
	
	public function refreshBibliotecas(){
		/*
		_clip.createEmptyMovieClip("mcArr", _clip.getNextHighestDepth());
		var menu1Mask:Rectangulo=new Rectangulo(_clip, 0,0, _acordion.width, 80, 0, 10);
		_clipArriba=_clip.mcArr;
		_mask=menu1Mask.getClip();
		_clipArriba.setMask(_mask);
				
		_bibliotecas=MediaCenter.getUsuario().getAlbums();
		
		for(var i:Number=0;i<_bibliotecas.length;i++){
			//trace(_bibliotecas[i].getNombre()+" "+_bibliotecas[i].getFotos().length);
			var mcBiblio:MovieClip=_clipArriba.attachMovie("BibliotecaUsr", "mcB"+i,
														   _clipArriba.getNextHighestDepth());
			mcBiblio._y=20*(1*i)+5;
			mcBiblio.titulo_txt.autoSize="left";
			mcBiblio.titulo_txt.selectable=false;
			mcBiblio.titulo_txt.text=_bibliotecas[i].getNombre() + " (" + _bibliotecas[i].getFotos().length + ")";
			mcBiblio.album=_bibliotecas[i];
			_clipsBibliotecas.push(mcBiblio);
			var menu:MenuItem=new MenuItem(mcBiblio);
			lsMenu=new MenuItemListener();
				lsMenu.clipPress=function(clip:MovieClip){
					Application.selectAlbum(clip.album);
					for(var sNested:String in clip._parent){
						if(clip._parent[sNested]==clip){
							clip.gotoAndStop(2);
						} else {
							clip._parent[sNested].gotoAndStop(1);
						}
					}
				}
				lsMenu.clipRollOver=function(clip:MovieClip){
					clip.gotoAndStop(2);
				}
				lsMenu.clipRollOut=function(clip:MovieClip){
					if(clip.album!=Application.getAlbum()){
					clip.gotoAndStop(1);
					}
				}
			menu.addListener(lsMenu);
			
			////////////////////////////////////////////////INICIALIZO EL MOVIE CLIP
		}
		chequearScroll();
		*/
	}
	
	private function dibujarMenu(){
		
		_clip.attachMovie("BibliotecaAdd", "mcAdd", _clip.getNextHighestDepth());
		var nCoordenadaMenuFijo:Number=_acordion.height - (21.5 * _acordion.numChildren) - 40 ;
		_clip.mcAdd._y= nCoordenadaMenuFijo;
		_clip.attachMovie("BibliotecaSupr", "mcSupr", _clip.getNextHighestDepth());
		_clip.mcSupr._y= _clip.mcAdd._y+20;
		var miAdd:MenuItem=new MenuItem(_clip.mcAdd);
		var miSupr:MenuItem=new MenuItem(_clip.mcSupr);
		var lsMenu2:MenuItemListener=new MenuItemListener();
		var funcionando:Boolean=false;
		var lBTarget:Object=new Object();
		lBTarget.terminar=function(){
				funcionando=false;
			}
		lsMenu2.clipRollOver=function(target:MovieClip){
			if(!funcionando){
			var bTarget:Barredor=new Barredor(target.mcColor);
			bTarget.addListener(lBTarget);
			funcionando=true;
				}
			}
		lsMenu2.clipPress=function(target:MovieClip){
			switch(target._name){
				case "mcAdd":
				Application.loadContent("crearAlbum.swf");
				break;
				case "mcSupr":
				if(Application.getAlbum()==undefined){
				 	//Application.setError("Seleccione una biblioteca");
					Application.loadContent("alertError.swf");
					break;
				} else {
					Application.loadContent("alertAlbum.swf");
					break;
				}
			}
		}
		miAdd.addListener(lsMenu2);
		miSupr.addListener(lsMenu2);
	}
	
	private function chequearScroll(){
		var bMenuScroll:Boolean=_clip.mcArr._height > _mask._height;
		if(bMenuScroll){
			var nDiferencia:Number= -_clipArriba._height + _mask._height;
			var scrollUp:MovieClip=_clipArriba.attachMovie("FlechaUp", "mcUp", _clipArriba.getNextHighestDepth());
			scrollUp._x=_acordion.width-20;
			scrollUp._y+=7;
			scrollUp.root=this;
			scrollUp.onPress=function(){
				if(this.root._clipArriba._y < 0 || this.root._clipArriba._y==2){
				this.root._clipArriba._y+=20;
				this._parent.mcBarraScroll._y-= (20 + cuenta);
				this._y-=20;
				scrollDown._y-=20;
				}
			}
			var scrollDown:MovieClip=_clipArriba.attachMovie("FlechaDown", "mcDown",
															 _clipArriba.getNextHighestDepth());
			scrollDown._x=_acordion.width-20;
			scrollDown._y=_mask._height-13.2;
			scrollDown.root=this;
			scrollDown.onPress=function(){
				if(this.root._clipArriba._y > nDiferencia){
				this.root._clipArriba._y-=20;
				this._parent.mcBarraScroll._y+= (20 + cuenta);
				this._y+=20;
				scrollUp._y+=20;
				}
			}
			var barraScroll:MovieClip=_clipArriba.attachMovie("BarraScroll", "mcBarraScroll", _clipArriba.getNextHighestDepth());
			barraScroll._x=scrollUp._x+(scrollUp._width/2);
			barraScroll._y=scrollUp._y+scrollUp._height+2;
			var algo:Number=Math.round(-nDiferencia / 20); 
			var barraLength:Number= scrollDown._y -(scrollUp._y + scrollUp._height) - 5;
			var cuenta:Number= barraLength / algo;
			
		}
	}

		
} // FIN